<?php
// 简单的Token生成脚本

// 获取用户ID，默认为1
$userId = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 1;

// 查询用户信息
try {
    // 数据库配置
    $config = [
        'hostname' => 'localhost',
        'database' => 'api_mh',  // 请修改为您的数据库名
        'username' => 'root',     // 请修改为您的数据库用户名
        'password' => 'root',     // 请修改为您的数据库密码
        'charset'  => 'utf8mb4',
        'port'     => 3306,
    ];
    
    // 连接数据库
    $dsn = "mysql:host={$config['hostname']};port={$config['port']};dbname={$config['database']};charset={$config['charset']}";
    $pdo = new PDO($dsn, $config['username'], $config['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
    ]);
    
    // 查询用户信息
    $stmt = $pdo->prepare("SELECT * FROM user WHERE id = ?");
    $stmt->execute([$userId]);
    $user = $stmt->fetch();
    
    if (!$user) {
        throw new Exception("找不到ID为 {$userId} 的用户");
    }
    
    // 使用真实的openid生成token
    $openid = $user['openid'];
} catch (Exception $e) {
    // 如果有错误，使用默认的测试openid
    $openid = 'test_openid_' . $userId;
}

// 模拟生成token
$token = base64_encode(json_encode([
    'openid' => $openid,
    'id' => $userId,
    'time' => time()
]));

// 输出token信息
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>API测试工具</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 20px;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .token-box {
            background-color: #f0f0f0;
            padding: 10px;
            border-radius: 5px;
            margin: 20px 0;
            word-break: break-all;
        }
        .auth-header {
            background-color: #e0f0e0;
            padding: 10px;
            border-radius: 5px;
            margin: 20px 0;
            word-break: break-all;
        }
        .endpoints {
            margin-top: 30px;
        }
        .endpoint {
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        .method {
            display: inline-block;
            padding: 3px 6px;
            background-color: #0066cc;
            color: white;
            border-radius: 3px;
            font-size: 0.8em;
            margin-right: 10px;
        }
        .get { background-color: #009900; }
        .post { background-color: #0066cc; }
        h2 {
            margin-top: 30px;
        }
        code {
            background-color: #f5f5f5;
            padding: 2px 5px;
            border-radius: 3px;
            font-family: monospace;
            font-size: 0.9em;
        }
        button {
            background-color: #0066cc;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 3px;
            cursor: pointer;
        }
        button:hover {
            background-color: #004499;
        }
        .user-info {
            background-color: #f9f9f9;
            padding: 10px;
            border-radius: 5px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <h1>API测试工具</h1>
    
    <?php if (isset($user)): ?>
    <div class="user-info">
        <h3>用户信息</h3>
        <p><strong>ID:</strong> <?php echo $user['id']; ?></p>
        <p><strong>OpenID:</strong> <?php echo $user['openid']; ?></p>
        <p><strong>昵称:</strong> <?php echo $user['nickname']; ?></p>
        <p><strong>代理等级:</strong> V<?php echo $user['agent_level']; ?></p>
    </div>
    <?php endif; ?>
    
    <h2>认证Token</h2>
    <p>使用以下Bearer Token进行API认证：</p>
    
    <div class="token-box" id="token"><?php echo $token; ?></div>
    <button onclick="copyToken()">复制Token</button>
    
    <h2>Authorization请求头</h2>
    <p>在API请求中添加以下请求头：</p>
    
    <div class="auth-header" id="auth">Authorization: Bearer <?php echo $token; ?></div>
    <button onclick="copyAuth()">复制Authorization头</button>
    
    <h2>API端点测试</h2>
    <div class="endpoints">
        <div class="endpoint">
            <span class="method get">GET</span>
            <code>/api/user/info</code> - 获取当前用户信息
        </div>
        
        <div class="endpoint">
            <span class="method get">GET</span>
            <code>/api/system/settings</code> - 获取系统设置
        </div>
        
        <div class="endpoint">
            <span class="method post">POST</span>
            <code>/api/note/submit</code> - 提交纸条
            <div>
                <pre>
{
  "nickname": "测试用户",
  "avatar": "https://example.com/avatar.jpg",
  "gender": 1,
  "age": 28,
  "city": "北京",
  "wechat_id": "test_wxid",
  "wechat_qrcode": "https://example.com/qrcode.jpg"
}
                </pre>
            </div>
        </div>
        
        <div class="endpoint">
            <span class="method post">POST</span>
            <code>/api/note/draw</code> - 抽取纸条
            <div>
                <pre>
{
  "count": 1,
  "gender": 2,
  "min_age": 18,
  "max_age": 30,
  "city": "上海"
}
                </pre>
            </div>
        </div>
        
        <div class="endpoint">
            <span class="method get">GET</span>
            <code>/api/note/my_notes</code> - 获取我的纸条
        </div>
        
        <div class="endpoint">
            <span class="method get">GET</span>
            <code>/api/note/my_draws</code> - 获取我抽取的纸条
        </div>
        
        <div class="endpoint">
            <span class="method get">GET</span>
            <code>/api/user/team</code> - 获取我的团队
        </div>
        
        <div class="endpoint">
            <span class="method get">GET</span>
            <code>/api/user/commissions</code> - 获取佣金记录
        </div>
        
        <div class="endpoint">
            <span class="method post">POST</span>
            <code>/api/user/bind_parent</code> - 绑定上级代理
            <div>
                <pre>
{
  "invite_code": "2"  // 上级用户ID
}
                </pre>
            </div>
        </div>
    </div>
    
    <h2>工具使用说明</h2>
    <ol>
        <li>复制上面生成的Token或Authorization头</li>
        <li>使用Postman、cURL或其他API测试工具发送请求</li>
        <li>确保在请求头中包含Authorization头信息</li>
        <li>根据需要发送GET或POST请求到相应端点</li>
    </ol>
    
    <script>
        function copyToken() {
            const token = document.getElementById('token').innerText;
            navigator.clipboard.writeText(token).then(() => {
                alert('Token已复制到剪贴板');
            });
        }
        
        function copyAuth() {
            const auth = document.getElementById('auth').innerText;
            navigator.clipboard.writeText(auth).then(() => {
                alert('Authorization头已复制到剪贴板');
            });
        }
    </script>
</body>
</html> 